package cn.xorange.love.controller;

import cn.hutool.core.util.RandomUtil;
import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.bean.SystemCode;
import cn.xorange.commons.core.basic.controller.BaseController;
import cn.xorange.commons.security.annotation.PassToken;
import cn.xorange.love.entity.AppUserPhoto;
import cn.xorange.love.entity.AppUserProfile;
import cn.xorange.love.entity.AppUserPush;
import cn.xorange.love.service.AppUserPhotoService;
import cn.xorange.love.service.AppUserProfileService;
import cn.xorange.love.service.AppUserPushService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author : yangjian
 * date : 2024/5/31
 * description :
 */
@RestController
@RequestMapping("/app/user/push")
public class AppUserPushController extends BaseController {

    @Resource
    AppUserPushService service;
    @Resource
    AppUserPhotoService photoService;
    @Resource
    AppUserProfileService profileService;

    @PostMapping()
    public ResultBean setPush(@RequestBody AppUserPush push){
        if(Objects.isNull(push.getUserId())){
            return ResultBean.onFail(SystemCode.USER_PARAM_NULL);
        }
        // 有效期3天
        int validity = 3;
        Date currentDate = new Date(); // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate); // 将当前日期设置到Calendar对象中
        calendar.add(Calendar.DAY_OF_YEAR, validity);
        push.setValidity(calendar.getTime());
        service.add(push);
        return ResultBean.onSuccess();
    }

    @PassToken
    @GetMapping("/{sex}")
    public ResultBean getPush(@PathVariable String sex){
        AppUserPush push = new AppUserPush();
        push.where.gte("validity",new Date());
        push.where.equal("sex", sex);
        List<AppUserPush> lst = this.service.getList(push);

        if(lst.isEmpty()){
            return ResultBean.onSuccess();
        }else {
            int randomIntBound = RandomUtil.randomInt(lst.size());
            return ResultBean.onSuccess(lst.get(randomIntBound));
        }
    }

    @PassToken
    @GetMapping("user/{userId}")
    public ResultBean getPushUser(@PathVariable Long userId){
        AppUserProfile user = new AppUserProfile();
        user.setUserId(userId);
        user = this.profileService.getEntity(user);

        AppUserPhoto photo = new AppUserPhoto();
        photo.setUserId(userId);
        photo.setSelectLimit(1);
        List<AppUserPhoto> lst = this.photoService.getList(photo);
        JSONObject u = new JSONObject();
        u.put("nickname", user.getNickname());
        u.put("birthday",user.getBirthday());
        u.put("zodiac", user.getZodiac());
        u.put("star", user.getStar());
        u.put("sex", user.getSex());
        u.put("relation", user.getRelation());
        u.put("height", user.getHeight());
        u.put("weight", user.getWeight());
        u.put("school", user.getSchool());
        u.put("education", user.getEducation());
        u.put("priovince", user.getProvince());
        u.put("city", user.getCity());
        u.put("lifeProvince", user.getLifeProvince());
        u.put("lifeCity", user.getLifeCity());
        u.put("lifeArea", user.getLifeArea());
        u.put("professionType", user.getProfessionType());
        u.put("income", user.getIncome());
        JSONObject obj = new JSONObject();
        obj.put("user", u);
        obj.put("photo", lst);
        return ResultBean.onSuccess(obj);
    }

}
